import * as constants from './constants';
import { fromJS } from 'immutable';
import { message } from 'antd';
import { getAllTaskAxios, runTaskAxios, deleteTaskAxios, stopTaskAxios, pauseTaskAxios, 
         getHistoryTaskAxios, getHistoryTaskLogAxios, getTaskInfoAxios
 } from '../../../../api/dataCollection/taskManage'; 
import Search from 'antd/lib/transfer/search';

 //任务监控
const getTaskList = (tasks) => ({
  type: constants.GET_TASK_LIST,
  tasks: fromJS(tasks),
});

export const getTaskListOperation = (username) => {//获取未完成任务列表
  return(dispatch) => {
    getAllTaskAxios(username).then((res) => {
      if(res){
        if(res.data.ok){
          const tasks = res.data.data;
          dispatch(getTaskList(tasks));
        }
      }
    });
  }
};

export const getTaskListSearch = (username, search) => {//搜索未完成任务列表
  return(dispatch) => {
    getAllTaskAxios(username).then((res) => {
      if(res){
        if(res.data.ok){
          const tasks = res.data.data;
          let searchTask = [];
          for(let i in tasks){
            if(tasks[i].taskName.indexOf(search) >= 0){
              searchTask.push(tasks[i])
            }
          }
          dispatch(getTaskList(searchTask));
        }
      }
    });
  }
};

export const runTaskOperation = (taskId) => {//执行任务
  return(dispatch) => {
    runTaskAxios(taskId).then((res) => {
      if(res){
        if(res.data.ok){
          message.success("任务执行成功!");
          dispatch(getTaskListOperation("admin"));//待修改
        }
      }
    });
  }
};

export const deleteTaskOperation = (taskId) => {//删除任务
  return(dispatch) => {
    deleteTaskAxios(taskId).then((res) => {
      if(res){
        if(res.data.ok){
          message.success("任务删除成功!");
          dispatch(getTaskListOperation("admin"));//待修改
        }
      }
    });
  }
};

export const pauseTaskOperation = (taskId) => {//暂停/重启任务
  return(dispatch) => {
    pauseTaskAxios(taskId).then((res) => {
      if(res){
        if(res.data.ok){
          message.success("任务暂停/重启成功!");
          dispatch(getTaskListOperation("admin"));//待修改
        }
      }
    });
  }
};

export const stopTaskOperation = (taskId) => {//停止执行任务
  return(dispatch) => {
    stopTaskAxios(taskId).then((res) => {
      if(res){
        if(res.data.ok){
          message.success("任务停止成功!");
          dispatch(getTaskListOperation("admin"));//待修改
        }
      }
    });
  }
};


//历史任务
const getHistoryTaskList = (tasks) => ({
  type: constants.GET_HISTORY_TASK_LIST,
  tasks: fromJS(tasks),
});

export const getHistoryTaskListOperation = (username) => {//获取历史任务列表
  return(dispatch) => {
    getHistoryTaskAxios(username).then((res) => {
      if(res){
        if(res.data.ok){
          const tasks = res.data.data;
          dispatch(getHistoryTaskList(tasks));
        }
      }
    });
  }
};

export const getHistoryTaskListSearch = (username, search) => {//搜索历史任务列表
  return(dispatch) => {
    getHistoryTaskAxios(username).then((res) => {
      if(res){
        if(res.data.ok){
          const tasks = res.data.data;
          let searchTask = [];
          for(let i in tasks){
            if(tasks[i].taskName.indexOf(search) >= 0){
              searchTask.push(tasks[i])
            }
          }
          dispatch(getHistoryTaskList(searchTask));
        }
      }
    });
  }
};



const getTaskLog = (taskLog) => ({
  type: constants.GET_TASK_LOG,
  taskLog: fromJS(taskLog)
})

export const getTaskLogOperation = (taskId) => {//获取任务日志
  return(dispatch) => {
    getHistoryTaskLogAxios(taskId).then((res) => {
      if(res){
        if(res.data.ok){
          const taskLog = res.data.data;
          dispatch(getTaskLog(taskLog));
        }
      }
    });
  }
};

const getTaskInfo = (taskInfo) => ({
  type: constants.GET_TASK_INFO,
  taskInfo: fromJS(taskInfo)
})

export const getTaskInfoOperation = (taskId) => {//获取任务详情
  return(dispatch) => {
    getTaskInfoAxios(taskId).then((res) => {
      if(res){
        if(res.data.ok){
          const taskInfo = res.data.data;
          console.log('任务详情',taskInfo);
          dispatch(getTaskInfo(taskInfo));
        }
      }
    });
  }
};


